﻿using Li.Contracts.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Li.Contracts.OutputDto;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.AspNetCore.Mvc.Authorization;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Li.Web.Providers
{
    public class MyAuthorizeFilter : Attribute,IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            if (context.Filters.Any(item => item is IAllowAnonymousFilter))
            {
                return;
            }
            var userInfo = context.HttpContext.Session.GetString("CurrentLoginUser");
            if (!string.IsNullOrEmpty(userInfo))
            {
                UserDto admin = JsonConvert.DeserializeObject<UserDto>(userInfo);
            }
            else
            {
                context.Result = new RedirectToActionResult("Login", "Access", null);
            }

        }
        //private static AuthorizationPolicy _policy_ = new AuthorizationPolicy(new[] { new DenyAnonymousAuthorizationRequirement() }, new string[] { });

        //public MyAuthorizeFilter() : base(_policy_) { }


        //public override  Task OnAuthorizationAsync(AuthorizationFilterContext context)
        //{

        //    if (context.Filters.Any(item=>item is IAllowAnonymousFilter))
        //    {

        //        return base.OnAuthorizationAsync(context);
        //    }
        //    else
        //    {
        //        var userInfo = context.HttpContext.Session.GetString("CurrentLoginUser");
        //        if (!string.IsNullOrEmpty(userInfo))
        //        {
        //            UserDto admin = JsonConvert.DeserializeObject<UserDto>(userInfo);
        //        }
        //        else
        //        {
        //            context.Result= new RedirectToActionResult("Login", "Access", null);
        //        }
        //         //base.OnAuthorizationAsync(context);
        //        return base.OnAuthorizationAsync(context);
        //    }
        //}
    }
}
